package com.sm.filemanagelibrary.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import androidx.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sm.filemanagelibrary.R;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * created by zhud on 2019/9/27
 */
public class TabSelectedLayout extends LinearLayout {
    private List<String> meunList;
    private int defaultBack = R.drawable.bg_tab_selected;

    private int normalTextColor = Color.WHITE;
    private int selectedTextColor = Color.BLACK;

    private int textSize = 40;
    private List<TextView> views = new ArrayList<>();
    private OnTabItemClickListener onTabItemClickListener;
    private int textPaddingleft = 30;
    private int textPaddingRight = 30;
    private int textPaddingTop = 10;
    private int textPaddingBottom = 10;

    public TabSelectedLayout(Context context) {
        this(context, null);
    }

    public TabSelectedLayout(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TabSelectedLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.TabSelectedLayout);
        String meunStr = array.getString(R.styleable.TabSelectedLayout_meunList);
        if (TextUtils.isEmpty(meunStr)) return;
        normalTextColor = array.getColor(R.styleable.TabSelectedLayout_normalTextColor, normalTextColor);
        selectedTextColor = array.getColor(R.styleable.TabSelectedLayout_selectedTextColor, selectedTextColor);
        textSize = array.getDimensionPixelSize(R.styleable.TabSelectedLayout_textSize, textSize);
        textPaddingleft = array.getDimensionPixelSize(R.styleable.TabSelectedLayout_textPaddingleft, textPaddingleft);
        textPaddingRight = array.getDimensionPixelSize(R.styleable.TabSelectedLayout_textPaddingRight, textPaddingRight);
        textPaddingTop = array.getDimensionPixelSize(R.styleable.TabSelectedLayout_textPaddingTop, textPaddingTop);
        textPaddingBottom = array.getDimensionPixelSize(R.styleable.TabSelectedLayout_textPaddingBottom, textPaddingBottom);
        defaultBack = array.getResourceId(R.styleable.TabSelectedLayout_selectedBackground, defaultBack);
        setOrientation(HORIZONTAL);
        meunList = new ArrayList<>(Arrays.asList(meunStr.split("\\|")));
        initView();
    }

    private void initView() {
        removeAllViews();
        for (int i = 0; i < meunList.size(); i++) {
            final String meunStr = meunList.get(i);
            FrameLayout layout = new FrameLayout(getContext());
            layout.setLayoutParams(new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, 1));
            TextView textView = new TextView(getContext());
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            params.gravity = Gravity.CENTER;
            textView.setLayoutParams(params);
            textView.setText(meunStr);
            textView.setTextColor(normalTextColor);
            textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
            textView.setPadding(textPaddingleft, textPaddingTop, textPaddingRight, textPaddingBottom);
            views.add(textView);
            layout.addView(textView);
            final int index = i;
            layout.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    updateSelectedState(index);
                    if (onTabItemClickListener != null) {
                        onTabItemClickListener.OnTabItemClick(index, meunStr);
                    }
                }
            });
            addView(layout);
        }
    }

    public void setOnTabItemClickListener(OnTabItemClickListener onTabItemClickListener) {
        this.onTabItemClickListener = onTabItemClickListener;
    }

    public void selected(int index) {
        updateSelectedState(index);
        if (onTabItemClickListener != null) {
            onTabItemClickListener.OnTabItemClick(index, meunList.get(index));
        }
    }

    private void updateSelectedState(int index) {
        for (int i = 0; i < views.size(); i++) {
            TextView textView = views.get(i);
            textView.setTextColor(i == index ? selectedTextColor : normalTextColor);
            textView.setBackgroundResource(i == index ? defaultBack : 0);
        }
    }

    public interface OnTabItemClickListener {
        void OnTabItemClick(int index, String meun);
    }
}
